package com.sky.lightleafpicturepavilionserver.controller.admin;

import com.sky.lightleafpicturepavilionserver.common.Result;
import com.sky.lightleafpicturepavilionserver.domain.WallpaperCategory;
import com.sky.lightleafpicturepavilionserver.service.WallpaperCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 管理端壁纸分类控制器
 */
@Tag(name = "管理端-壁纸分类管理", description = "管理端壁纸分类CRUD接口")
@Slf4j
@RestController
@RequestMapping("/api/v1/admin/wallpaper-category")
public class AdminWallpaperCategoryController {

    @Resource
    private WallpaperCategoryService wallpaperCategoryService;

    /**
     * 创建分类
     */
    @Operation(summary = "创建分类", description = "创建新的壁纸分类")
    @PostMapping
    public Result<String> createCategory(@RequestBody WallpaperCategory category) {
        log.info("创建分类：{}", category.getName());
        
        // 检查分类名称是否重复
        WallpaperCategory existCategory = wallpaperCategoryService.lambdaQuery()
                .eq(WallpaperCategory::getName, category.getName())
                .one();
        if (existCategory != null) {
            return Result.error("分类名称已存在");
        }
        
        boolean success = wallpaperCategoryService.save(category);
        if (success) {
            log.info("分类创建成功，ID：{}", category.getId());
            return Result.success("分类创建成功");
        } else {
            return Result.error("分类创建失败");
        }
    }

    /**
     * 更新分类
     */
    @Operation(summary = "更新分类", description = "更新壁纸分类信息")
    @PutMapping("/{categoryId}")
    public Result<String> updateCategory(
            @Parameter(description = "分类ID") @PathVariable Long categoryId,
            @RequestBody WallpaperCategory category) {
        log.info("更新分类，ID：{}，名称：{}", categoryId, category.getName());
        
        // 检查分类是否存在
        WallpaperCategory existCategory = wallpaperCategoryService.getById(categoryId);
        if (existCategory == null) {
            return Result.error("分类不存在");
        }
        
        // 检查分类名称是否重复（排除自己）
        WallpaperCategory duplicateCategory = wallpaperCategoryService.lambdaQuery()
                .eq(WallpaperCategory::getName, category.getName())
                .ne(WallpaperCategory::getId, categoryId)
                .one();
        if (duplicateCategory != null) {
            return Result.error("分类名称已存在");
        }
        
        category.setId(categoryId);
        boolean success = wallpaperCategoryService.updateById(category);
        if (success) {
            log.info("分类更新成功");
            return Result.success("分类更新成功");
        } else {
            return Result.error("分类更新失败");
        }
    }

    /**
     * 删除分类
     */
    @Operation(summary = "删除分类", description = "删除壁纸分类")
    @DeleteMapping("/{categoryId}")
    public Result<String> deleteCategory(@Parameter(description = "分类ID") @PathVariable Long categoryId) {
        log.info("删除分类，ID：{}", categoryId);
        
        // 检查分类是否存在
        WallpaperCategory category = wallpaperCategoryService.getById(categoryId);
        if (category == null) {
            return Result.error("分类不存在");
        }
        
        // TODO: 检查是否有关联的壁纸，如果有则不允许删除
        // 这里可以根据业务需求决定是否允许删除有壁纸的分类
        
        boolean success = wallpaperCategoryService.removeById(categoryId);
        if (success) {
            log.info("分类删除成功");
            return Result.success("分类删除成功");
        } else {
            return Result.error("分类删除失败");
        }
    }

    /**
     * 获取分类详情（管理端）
     */
    @Operation(summary = "获取分类详情", description = "获取分类完整信息（管理端）")
    @GetMapping("/{categoryId}")
    public Result<WallpaperCategory> getCategoryById(@Parameter(description = "分类ID") @PathVariable Long categoryId) {
        log.info("获取分类详情，ID：{}", categoryId);
        
        WallpaperCategory category = wallpaperCategoryService.getById(categoryId);
        if (category == null) {
            return Result.error("分类不存在");
        }
        
        return Result.success(category);
    }
}